package lovemusic.lanou.com.newsmusic.utils;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;



import java.util.ArrayList;
import java.util.List;

import lovemusic.lanou.com.newsmusic.models.MusicInfo;

/**
 * 定义一个扫描本地歌曲的工具类
 * Created by lanouhn on 16/9/22.
 */
public class MusicUtils {

    public static List getMusicDate(){
        List list=new ArrayList();
        list.clear();
        Cursor cursor=UIUtils.getContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        if (cursor!=null) while (cursor.moveToNext()) {
            int isMusic=cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
            if (isMusic==0){
                continue;
            }

            long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
            String title = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String unknown = "未知";
            artist = artist.equals("<unknown>") ? unknown : artist;
            String album = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
            long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
            String uri = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

            String coverUri = getCoverUri(albumId);
            String fileName = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
            long fileSize = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
            String year = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.YEAR)));

            MusicInfo musicInfo = new MusicInfo();


            musicInfo.setId(id);
            musicInfo.setType(MusicInfo.Type.LOCAL);
            musicInfo.setTitle(title);
            musicInfo.setArtist(artist);
            musicInfo.setAlbum(album);
            musicInfo.setDuration(duration);
            musicInfo.setUri(uri);
            musicInfo.setCoverUri(coverUri);
            musicInfo.setFileName(fileName);
            musicInfo.setFileSize(fileSize);
            musicInfo.setYear(year);

            if (musicInfo.getFileSize()>1000*80){
                if (musicInfo.getTitle().contains("-")){
                    String[] str=musicInfo.getTitle().split("-");
                    musicInfo.setArtist(str[0]);
                    musicInfo.setTitle(str[1]);
                }
                list.add(musicInfo);
            }
        }
        cursor.close();
        return list;

    };

    private static String getCoverUri(long albumId) {
        String uri = null;
        Cursor cursor = UIUtils.getContext().getContentResolver().query(
                Uri.parse("content://media/external/audio/albums/" + albumId),
                new String[]{"album_art"}, null, null, null);
        if (cursor != null) {
            cursor.moveToNext();
            uri = cursor.getString(0);
            cursor.close();
        }
        CoverLoader.getInstance().loadThumbnail(uri);
        return uri;
    }


}
